# 前3位—网络识别号；第4-7位—地区编码；第8-11位—用户号码。


# 随机生成手机号码
import random
import time
import requests
from lxml import etree
from multiprocessing import Pool
from mysqlphonenumber import MysqlHelp
from get_user_agent import User_Agent_Help


my=MysqlHelp()
ua=User_Agent_Help()


class Bequtiful_Phone(object):


    #初始化对象，为对象添加url和headers属性
    def __init__(self):
        self.url='http://www.ip138.com:8080/search.asp?mobile={}&action=mobile'
        self.headers={
            'Cookie': 'Hm_lvt_b018ba5033f3b0d184416653ad858a48=1575266891; Hm_lvt_f4f76646cd877e538aa1fbbdf351c548=1575266871,1575267050; ASPSESSIONIDACQSDATT=OEIBPGKAKDCGKCPDHPFKOPHM; Hm_lvt_ecdd6f3afaa488ece3938bcdbb89e8da=1577263268,1577346163,1577680617,1577682052; Hm_lpvt_ecdd6f3afaa488ece3938bcdbb89e8da=1577682052',
            'Host': 'www.ip138.com:8080',
            'Referer': 'http://www.ip138.com:8080/search.asp',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
        }


    # 准备数据，为验证是否是哪个地区的电话号段做准备
    def prepare_data(self):
        first_part=['133','149','153','173','177','180','181','189','191','193','199','190','130','131','132','145','155','156','166','171','175','176','185','186','196','134','135','136','137','138','139','147','150','151','152','157','158','159','172','178','182','183','184','187','188','195','198','197']
        second_part=[str(i)+str(j)+str(m)+str(n) for i in range(10) for j in range(10) for m in range(10) for n in range(10)]
        for i in first_part:
            for j in second_part:
                half_phone=i+j
                yield half_phone

                
    # 根据准备的数据，为验证的号段的网址，发送请求
    def send_request(self,url):
        print(url)
        try:
            response=requests.get(url,headers=self.headers)
            print(self.headers['User-Agent'])
            print(response.status_code)
            response.encoding='gb2312'
            
            return response.content
        except:
            time.sleep(random.randint(120,240))
            self.send_request(url)
        

    # 过滤数据，把想要的数据song网页内容中取出
    def filter_data(self,data):
        phone=[]
        # print(data)
        html_ele=etree.HTML(data)
        half_part=html_ele.xpath('//table/tr[2]/td[2]/text()')[0].strip()
        provinces=html_ele.xpath('//table/tr[3]/td[2]/text()')[0].split(' ')[0].strip()
        city=html_ele.xpath('//table/tr[3]/td[2]/text()')[0].split(' ')[-1].strip()
        card_type=html_ele.xpath('//table/tr[4]/td[2]/text()')[0].strip()
        if provinces:
            phone.append(half_part)
            phone.append(provinces)
            phone.append(city)
            phone.append(card_type)
        return phone


    # 保存数据，加你个想要的数据存储到指定文件里
    # 一个城市的写入到一起，表名叫省加城市名
    def save_data(self,data):
        sql='select * from phone_number where number_segment=%s'
        if data and (not my.execute_sql(sql=sql,data=data[0],mode='r')):
            sql='insert into phone_number(number_segment,provinces,city,card_type) values(%s,%s,%s,%s)'
            if my.execute_sql(sql,data,mode='w'):
                print('存储成功')
            else:
                print('存储失败')
        else:
            print('数据已存在')
        
        





        # # 创建一个新的工作簿
        # book=xlwt.Workbook(encoding='utf-8',style_compression=0)
        # # 在工作簿里创建工作表
        # sheet=book.add_sheet('shenyang',cell_overwrite_ok=True)
        # # 行列值
        # for i in range(len(phone_list)):
        #     sheet.write(i,0,phone_list[i])
            
        # # 保存工作簿到指定路径里
        # book.save('./phone.xls')


    # 运行函数，所有的函数都在此处被调用
    def run(self):
        phone_list=[]
        for half_phone in self.prepare_data():
            url=self.url.format(half_phone)
            self.headers['User-Agent']=ua.get_useragent()
            time.sleep(random.randint(5,9))
            data=self.send_request(url)
            phone_info=self.filter_data(data)
            self.save_data(phone_info)

    # def process_speed(self):
    #     p=Pool(3)
    #     p.apply_async(func=self.run)
    #     p.close()
    #     p.join()


# 程序的入口
if __name__ == '__main__':
    bphone=Bequtiful_Phone()
    bphone.run()
